<template>
  <base-layout
    :dialog-title="dialogTitle"
    :dialog-close-confirm="false"
    :dialog-visible="dialogVisible"
    @dialog-close="dialogClose"
    dialogWidth="80%"
  >
    <template #searchPanel>
      <search-panel
        ref="searchPanel"
        v-slot="{ form }"
        @search="search"
        :selectPer="authConfig.query"
        addTitle="新增退货"
        @insert="addHandle"
        :addPer="authConfig.add"
      >
        <job-shop v-model="form.storeId" placeholder="所属门店" />
        <el-select v-model="form.status" clearable placeholder="退货状态">
          <el-option v-for="item in dict['退货状态']" :key="item.index" :value="item.value" :label="item.text"/>
        </el-select>
        <el-input
          v-model="form.code"
          clearable
          placeholder="退货编号"
          style="width: 200px"
        />
      </search-panel>
    </template>

    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @row-dblclick="detailHandle"
        :width="160"
      >
        <template #action="{row}">
          <i
            class="el-icon-edit pointer"
            title="编辑"
            v-permission="authConfig.edit"
            @click="editHandle(row, '编辑')"
          ></i>
          <i
            class="el-icon-delete pointer ml-20"
            title="删除"
            v-permission="authConfig.del"
            @click="handleDataDelete(row)"
          ></i>
        </template>
      </data-table>
      <!--工具条-->
    </template>
    <template #dialog>
      <return-record-detail v-if="detailData" :detail-data="detailData" @refresh="setTableData"/>
    </template>
  </base-layout>
</template>

<script>
import baseMixin from '@/mixins/base'
import ReturnRecordDetail from './components/returnRecordDetail'
import { pageList, deleteById } from '@/api/pre-sale/boutiquePurchase/return'
import { getDict } from '@/utils'

export default {
  components: {
    ReturnRecordDetail
  },
  mixins: [
    baseMixin({
      findData: pageList,
      dataName: ['content', 'totalRecords'],
      updateData: deleteById,
      idName: 'id',
      format(res) {
        return res.map(data => {
          return data
        })
      }
    })
  ],
  data() {
    this.authConfig = {
      query: 'purchaseReturn:page',
      add: 'purchaseReturn:save',
      edit: 'purchaseReturn:update',
      del: 'purchaseReturn:del',
      return: 'purchaseReturn:save'
    }

    return {
      dialogVisible: false,
      dialogTitle: '',
      tableColumnAttributes: [
        { name: '退货门店', value: 'storeName', type: 'text' },
        { name: '退货编号', value: 'returnCode', type: 'text' },
        { name: '采购编号', value: 'purchaseCode', type: 'text' },
        { name: '退货类型', value: 'returnType', type: 'dict', dict: '退货类型' },
        { name: '退货供应商', value: 'supplierName', type: 'text' },
        { name: '退货负责人', value: 'userName', type: 'text' },
        { name: '退货负责人联系方式', value: 'phone', type: 'text' },
        { name: '退货总金额', value: 'amount', type: 'text' },
        { name: '退货备注', value: 'remark', type: 'text' },
        { name: '状态', value: 'status', type: 'text' },
        { name: '创建时间', value: 'createTime', type: 'text' }
      ],
      tableData: [],
      detailData: null
    }
  },
  methods: {
    addHandle() {
      this.$router.push({ name: 'boutiquePurchaseReturnAdd', params: { id: 0, returnType: '采购退货', purchaseCode: 0 }})
    },
    editHandle(row) {
      this.$router.push({ name: `boutiquePurchaseReturnEdit`, params: { id: row.id, returnType: row.returnType, purchaseCode: row.purchaseCode }})
    },
    detailHandle(row) {
      this.dialogTitle = '退货详情'
      this.dialogWidth = '1000px'
      this.detailData = row
      this.dialogVisible = true
    },
    approvalHandle(row) {
      this.dialogTitle = '审核'
      this.dialogWidth = '600px'
      this.approvalData = row
      this.dialogVisible = true
    },
    dialogClose() {
      this.dialogVisible = false
      this.detailData = null
    },
    search(form) {
      this.searchOptions = {
        storeId: form.storeId,
        status: form.status,
        code: form.code
      }
      this.currentPage = 1
      this.setTableData()
    }
  },
  created() {
    this.dict = getDict(['退货状态'])
  },
  mounted() {

  }
}
</script>

<style lang="scss" scoped></style>
